using Microsoft.AspNetCore.Mvc;
using RiceShop.Models;
using RiceShop.Services;

namespace RiceShop.Controllers
{
    /// <summary>
    /// 角色管理控制器
    /// 处理与角色相关的 HTTP 请求
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;

        /// <summary>
        /// 构造函数，通过依赖注入获取角色服务
        /// </summary>
        /// <param name="roleService">角色服务接口</param>
        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 获取所有角色列表
        /// </summary>
        /// <returns>返回角色列表</returns>
        [HttpGet("list")]
        public async Task<ActionResult<IEnumerable<Role>>> GetRoles()
        {
            var roles = await _roleService.GetAllRolesAsync();
            return Ok(ApiResponse<IEnumerable<Role>>.Ok(roles, "获取用户列表成功", roles.Count()));

        }

        /// <summary>
        /// 根据ID获取特定角色详情
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns>返回指定的角色信息，如果未找到则返回404</returns>
        [HttpGet("detail/{id}")]
        public async Task<ActionResult<Role>> GetRole(long id)
        {
            var role = await _roleService.GetRoleByIdAsync(id);
            if (role == null)
                return NotFound();

            return Ok(role);
        }

        /// <summary>
        /// 创建新角色
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns>返回创建成功的角色信息</returns>
        [HttpPost("create")]
        public async Task<ActionResult<Role>> CreateRole(Role role)
        {
            var createdRole = await _roleService.CreateRoleAsync(role);
            return CreatedAtAction(nameof(GetRole), new { id = createdRole.roleId }, createdRole);
        }

        /// <summary>
        /// 更新指定角色信息
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <param name="role">更新后的角色信息</param>
        /// <returns>返回更新后的角色信息，如果未找到则返回404</returns>
        [HttpPut]
        [Route("update/{id}")]
        public async Task<IActionResult> UpdateRole(long id, Role role)
        {
            var updatedRole = await _roleService.UpdateRoleAsync(id, role);
            if (updatedRole == null)
                return NotFound();

            return Ok(updatedRole);
        }

        /// <summary>
        /// 删除指定角色
        /// </summary>
        /// <param name="id">要删除的角色ID</param>
        /// <returns>删除成功返回204，未找到返回404</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteRole(long id)
        {
            var result = await _roleService.DeleteRoleAsync(id);
            if (!result)
                return NotFound();

            return NoContent();
        }


        /// <summary>
        /// 获取角色菜单权限列表。
        /// </summary>
        /// <param name="roleId">角色ID。</param>
        /// <returns>角色列表。</returns>
        [HttpGet("MenuPermissions")]
        public ActionResult<IEnumerable<long>> GetRoleMenuPermissions([FromQuery] long roleId)
        {
            var permissions = _roleService.GetRoleMenuPermissions(roleId);
            return Ok(ApiResponse<IEnumerable<long>>.Ok(permissions, "获取角色菜单权限列表成功"));


        }

        /// <summary>
        ///  分配角色菜单权限。
        /// </summary>
        /// <param name="model">分配角色菜单权限模型。</param>
        /// <returns>操作结果。</returns>
        [HttpPost("assignMenuPermissions")]
        public IActionResult AssignRole([FromBody] AssignRoleMenuModel model)
        {

            _roleService.AssignMenuPermissionsToRole(model);

            return NoContent();
        }



    }
}
